/**
 * 文件：ExtendListEditAdapter.java
 * 包名：com.ronri.list.editview;
 * 说明：可扩展编辑适配器
 * 作者：MS During
 * 日期：2017/5/8
 * 历史：
 */
package com.ronri.list.editview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseExpandableListAdapter;
import android.widget.Toast;

import com.cz.nongyetongv5.application.InitApplication;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

public class ExtendListEditAdapter
        extends BaseExpandableListAdapter
        implements AdapterView.OnItemClickListener {

    private static final String _TAG = "ExtendListEditAdapter";
    private List<AbstractItemElement> _mContentList;
    private LayoutInflater _mInflater;
    private ListEditAdapter.Callback _mCallback;

    private Context _mContext;

    /***
     * 构造类
     *
     * @param context     句柄
     * @param contentList 列表
     * @param callback    回调
     */
    public ExtendListEditAdapter(Context context,
                           List<AbstractItemElement> contentList,
                           ListEditAdapter.Callback callback) {
        _mContext = context;
        _mContentList = contentList;
        _mInflater = LayoutInflater.from(context);
        _mCallback = callback;
    }

    @Override
    public int getGroupCount() {
        return _mContentList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if(_mContentList.get(groupPosition).hasChildren()){
            return _mContentList.get(groupPosition).Childrens.size();
        }
        return 0;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return _mContentList.get(groupPosition).getTitle();
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return _mContentList.get(groupPosition).Childrens.get(childPosition).getTitle();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        //if (convertView == null) {
            convertView =_mContentList.get(groupPosition).CreateView(parent);
            convertView.setTag(_mContentList.get(groupPosition));
       // }

        AutoUtils.auto(convertView);

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        //if (convertView == null) {
            convertView = _mContentList.get(groupPosition).Childrens.get(childPosition).CreateView(parent);
            // convertView.setTag(_mContentList.get(groupPosition).Childrens.get(childPosition));
            AutoUtils.auto(convertView);
        //}else{
         //   convertView.destroyDrawingCache();
          //  convertView = _mContentList.get(groupPosition).Childrens.get(childPosition).CreateView(parent);
        //}

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    /***
     * Item点击控制
     * @param parent
     * @param view
     * @param position
     * @param id
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(InitApplication.getInstance().getBaseContext(), "adfadf", Toast.LENGTH_LONG);
    }
}
